package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.utils.JsonUtils;
import com.leyou.dto.SpecParamDTO;
import com.leyou.entity.SpecParam;
import com.leyou.entity.Spu;
import com.leyou.entity.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpecParamService specParamService;
    @Override
    public List<SpecParamDTO> queryparams(Integer id, Boolean searching) {
        SpuDetail byId = this.getById(id);
        //将细节表中的特别变为map集合
        Map<Long, Object> longObjectMap = JsonUtils.toMap(byId.getSpecification(), Long.class, Object.class);
        //取出属性集合
        //在赋值给属性集合
        Spu byId1 = spuService.getById(id);
        Long cid3 = byId1.getCid3();
        //得到spu
        //拿到spu中的商品分类查出所属的规格参数
        //然后再赋值进去
        List<SpecParam> querybyparam = specParamService.querybyparam(cid3, null, searching);
        List<SpecParamDTO> specParamDTOS = SpecParamDTO.convertEntityList(querybyparam);
        for (SpecParamDTO specParamDTO : specParamDTOS) {
            Long id1 = specParamDTO.getId();
            Object o = longObjectMap.get(id1);
            specParamDTO.setValue(o);
        }
        return specParamDTOS;

    }
}
